import { BasicForm } from '@/components/ProForm/interface/index';
import { PublicInterface } from '@/interface/public';

import { defaultStyle } from '@/basic/style';
import { UserInterface } from '@/api/interface/auth/user';

export function useAddForm(proTableBox: PublicInterface.ProTableValue) {
  const userStore = useUserStore();

  let addForm = reactive<BasicForm<UserInterface.ReqSave>>({
    form: {
      id: null,
      name: '',
      password: '',
      account: '',
      email: '',
      phone: '',
      gender: null,
      remark: ''
    },
    title: '',
    visible: false,
    transitionArr: [
      {
        oldField: 'gender',
        newField: 'gender'
      }
    ],
    options: [
      {
        label: 'id',
        prop: 'id',
        hide: true,
        type: 'input',
        defaultValue: ''
      },
      {
        type: 'input',
        label: '姓名',
        hide: false,
        prop: 'name',
        defaultValue: '',
        rules: [{ required: true, message: '请输入姓名', trigger: 'blur' }]
      },
      {
        type: 'input',
        label: '账号',
        hide: false,
        prop: 'account',
        defaultValue: '',
        rules: [{ required: true, message: '请输入账号', trigger: 'blur' }]
      },
      {
        type: 'input',
        label: '邮箱',
        hide: false,
        defaultValue: '',
        prop: 'email'
      },
      {
        type: 'input',
        label: '手机号',
        hide: false,
        defaultValue: '',
        prop: 'phone'
      },
      {
        type: 'select',
        label: '性别',
        hide: false,
        defaultValue: '',
        prop: 'gender',
        children: []
      },
      {
        type: 'input',
        label: 'remark',
        defaultValue: '',
        hide: false,
        prop: 'remark',
        attrs: {
          style: defaultStyle
        }
      }
    ]
  });

  const saveAddForm = async (form: UserInterface.ResUser) => {
    await userRegister(form);
    addForm.visible = false;
    elMessage();
    proTableBox.value?.getTableList();
  };

  return { addForm, saveAddForm };
}
